/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.test.mock.resource;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.omorphdb.core.io.Constants;
import org.omorphdb.core.io.Reader;

class ByteArrayReader implements Reader
{
	private final ByteBuffer buffer;

	ByteArrayReader(byte[] array)
	{
		this.buffer = ByteBuffer.wrap(array);
	}

	@Override
	public void setCursor(long position) throws IOException
	{
		this.buffer.position((int) position);
	}

	@Override
	public long getCursor() throws IOException
	{
		return this.buffer.position();
	}

	@Override
	public long shift(int amount) throws IOException
	{
		int newPosition = amount + this.buffer.position();
		this.buffer.position(newPosition);

		return newPosition;
	}

	@Override
	public void rewind() throws IOException
	{
		this.buffer.rewind();
	}

	@Override
	public String readString() throws IOException
	{
		return new String(readStringAsBytes());
	}

	@Override
	public byte[] readStringAsBytes() throws IOException
	{
		int l = this.buffer.getInt();
		byte[] dst = new byte[l];

		this.buffer.get(dst);

		return dst;
	}

	@Override
	public double readDouble() throws IOException
	{
		return this.buffer.getDouble();
	}

	@Override
	public float readFloat() throws IOException
	{
		return this.buffer.getFloat();
	}

	@Override
	public long readLong() throws IOException
	{
		return this.buffer.getLong();
	}

	@Override
	public int readInt() throws IOException
	{
		return this.buffer.getInt();
	}

	@Override
	public short readShort() throws IOException
	{
		return this.buffer.getShort();
	}

	@Override
	public char readChar() throws IOException
	{
		return this.buffer.getChar();
	}

	@Override
	public byte readByte() throws IOException
	{
		return this.buffer.get();
	}

	@Override
	public boolean readBoolean() throws IOException
	{
		return this.buffer.get() != Constants.FALSE;
	}

	@Override
	public byte readType() throws IOException
	{
		return readByte();
	}
}
